using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

	Transform player;
	public GameObject projetilInimigo;
	
	public float verticalVel;
	public float HorizontalVel;
	
	public float minDistanceToAttack;
	
	public float attackFrequency;
	public string mode;
	
	public float cronometro;
	
	void Awake () 
	{
		player = GameObject.FindGameObjectWithTag("GamePlayer").transform;
	}
	void Update () 
	{
		
		cronometro += Time.deltaTime;
		
		if(mode.Equals("AttackMode"))
		{
			if(player.transform.position.y > gameObject.transform.position.y)
			{
				transform.Translate(new Vector3(0, verticalVel,0)*Time.deltaTime);
			}
			if(player.transform.position.y < gameObject.transform.position.y)
			{
				transform.Translate(new Vector3(0, -verticalVel,0)*Time.deltaTime);
			}
			
			if(cronometro > attackFrequency)
			{
				Instantiate(projetilInimigo, transform.position, transform.rotation);
				cronometro = 0;
			}
			
		}
		if(mode.Equals("Non-AttackMode"))
		{
			if(player.transform.position.y > gameObject.transform.position.y)
			{
				transform.Translate(new Vector3(0, verticalVel,0)*Time.deltaTime);
			}
			if(player.transform.position.y < gameObject.transform.position.y)
			{
			
				transform.Translate(new Vector3(0, -verticalVel,0)*Time.deltaTime);
			}
			if(player.transform.position.x > transform.position.x)
			{
				transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, 0, transform.localEulerAngles.z);
				transform.Translate(new Vector3(HorizontalVel,0,0)*Time.deltaTime);
			}
			if(player.transform.position.x < transform.position.x)
			{
				transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, 180, transform.localEulerAngles.z);
				transform.Translate(new Vector3(HorizontalVel,0,0)*Time.deltaTime);
			}
			
		}
		
		if(Vector3.Distance(player.transform.position, transform.position) > minDistanceToAttack)
		{
			mode = "Non-AttackMode";
		}else
		{
			mode = "AttackMode";
		}
		
	}
	
	
}
